﻿// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'

// Upgrade NOTE: commented out 'float4 unity_LightmapST', a built-in variable
// Upgrade NOTE: commented out 'sampler2D unity_Lightmap', a built-in variable
// Upgrade NOTE: replaced tex2D unity_Lightmap with UNITY_SAMPLE_TEX2D

Shader "Custom/UnityLightMapEnvAlphaFlowShaderIllum" {
	Properties {
		_MainTex ("Base (RGB)", 2D) = "white" {}

		//_MaskTex ("Mask (RGB)", 2D) = "white" {}
		_FlowTex ("Flow (RGB)", 2D) = "white" {}

		_UVAnimX ("UV Anim X", float) = 0
		_UVAnimY ("UV Anim Y", float) = 0
		_Scale ("Scale", float) = 1
		_Scale2 ("Scale2", float) = 1

		_Color ("Color", Color) = (1, 1, 1, 1)
	}
	SubShader {
		Tags { 
			"RenderType" = "Transparent" 
			"Queue"="Transparent" 
			"PreviewType" = "Plane"
		}
		Pass {
			Lighting Off
			Blend SrcAlpha OneMinusSrcAlpha 
			CGPROGRAM
			  #pragma vertex vert
			  #pragma fragment frag
			  #include "UnityCG.cginc"



	      struct appdata_lightmap {
	        float4 vertex : POSITION;
	        float2 texcoord : TEXCOORD0;
	        float2 texcoord1 : TEXCOORD1;
	      };

		  //光照贴图需要模型有两套UV
	      struct v2f {
	        float4 pos : SV_POSITION;
	        float2 uv0 : TEXCOORD0;
	        float2 uv1 : TEXCOORD1;
			fixed3 shadowPos : TEXCOORD2;
			fixed3 offPos : TEXCOORD3;
			fixed2 uv2 : TEXCOORD4;
			//fixed2 uv3 : TEXCOORD5;
	      };

	      //unity光照贴图
	      // sampler2D unity_Lightmap;
	      //光照贴图的位置的缩放和偏移
	      // float4 unity_LightmapST;

	      //纹理动画需要ST
	      sampler2D _MainTex;
	      float4 _MainTex_ST; 


	      //sampler2D _MaskTex;
	      sampler2D _FlowTex;


		  //阴影相关
			uniform sampler2D _ShadowMap;
		    uniform float4 _ShadowCamPos;
		    uniform float _ShadowCameraSize;

			//光照蒙版 防止光照贴图边缘存在问题
			uniform sampler2D _LightMask;

			//动态光照
			uniform float _LightCoff;
		    uniform float4 _CamPos;
		    uniform float _CameraSize;
			uniform sampler2D _LightMap;
		    uniform float4 _AmbientCol;


			uniform fixed _UVAnimX;
	        uniform fixed _UVAnimY;
	        uniform fixed _Scale;
	        uniform fixed _Scale2;

			uniform fixed4 _Color;

	      v2f vert(appdata_lightmap i) {
	        v2f o;
	        o.pos = mul(UNITY_MATRIX_MVP, i.vertex);

	        o.uv0 = TRANSFORM_TEX(i.texcoord, _MainTex);
	        o.uv1 = i.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
			fixed3 worldPos = mul(unity_ObjectToWorld, i.vertex).xyz;
			o.shadowPos = worldPos-(_WorldSpaceCameraPos+_ShadowCamPos);

			o.offPos = worldPos-(_WorldSpaceCameraPos+_CamPos);

			float t1 = _Time.y*_UVAnimX;
			float t2 = _Time.y*_UVAnimY;
			o.uv2 = (i.texcoord+ fixed2(t1, t2))*_Scale;
			//o.uv3 = i.texcoord*_Scale2;
	        return o;
	      }

	      half4 frag(v2f i) : COLOR {
	        half4 main_color = tex2D(_MainTex, i.uv0);
	        main_color.rgb *= DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uv1));

			fixed2 mapShadow = (i.shadowPos.xz+float2(_ShadowCameraSize, _ShadowCameraSize))/(2*_ShadowCameraSize);
			fixed4 shadowC = tex2D(_ShadowMap, mapShadow);

			fixed2 mapUV = (i.offPos.xz+float2(_CameraSize, _CameraSize))/(2*_CameraSize);
			fixed3 lightCol = tex2D(_LightMap, mapUV).rgb * _LightCoff;
			 
			fixed mask = 1-tex2D(_LightMask, mapUV).a;

			main_color.rgb = (1-mask)*main_color.rgb+ mask*main_color.rgb * shadowC.rgb * (_AmbientCol.rgb+lightCol);

			//fixed4 maskCol = tex2D(_MaskTex, i.uv3);
			fixed4 flowCol = tex2D(_FlowTex, i.uv2);
			//fixed3 lightPoint = maskCol.r*flowCol.rgb;
			fixed3 lightPoint = flowCol.rgb;
			//main_color.rgb += lightPoint;
			// main_color.rgb = maskCol.rgb;
			//main_color.rgb = flowCol.rgb;
			// main_color.a = 1;	
			main_color.rgb += lightPoint.r*_Color;

	        return main_color;
	      }
	      ENDCG
		}
		
	} 
	FallBack "Diffuse"
}
